package com.aaa.controller.mht;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONConfig;
import cn.hutool.json.JSONUtil;
import com.aaa.controller.BaseController;
import com.aaa.entity.Result;
import com.aaa.entity.ReturnRecords;
import com.aaa.entity.mht.TestResultMht;
import com.aaa.entity.system.PageOrder;
import com.aaa.service.mht.TestResultMhtService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;


/**
 * (TestResultMht)表控制层
 *
 * @author makejava
 * @since 2022-09-03 14:43:00
 */
@RestController
@RequestMapping("testResultMht")
public class TestResultMhtController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private TestResultMhtService testResultMhtService;

    @RequestMapping("queryAll.do")
    public String queryAll(PageOrder pageOrder, TestResultMht testResultMht) {
        Page page = testResultMhtService.queryAll(pageOrder, testResultMht);
        JSONConfig jsonConfig = JSONConfig.create();
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        String jsonStr = JSONUtil.toJsonStr(success(page.getTotal(), page.getResult()), jsonConfig);
        return jsonStr;
    }

    /**
     * 雷达图
     * @param id
     * @return
     */
    @RequestMapping("queryChart.do")
    public Result queryChart(int id) {
        List<TestResultMht> list = testResultMhtService.queryChart(id);
        if(ObjectUtil.isNotEmpty(list)){
            for (TestResultMht testResultMht : list) {
                return success(list);
            }
        }
        return fail();
    }


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("deleteMht.do")
    public Result deleteById(int[] id) {
        int b = testResultMhtService.deleteById(id);
        if(b>0){
            return success();
        }else {
            return fail();
        }
    }

    /**
     * 结果显示页面
     * @param id
     * @return
     */
    @RequestMapping("queryResult.do")
    public Result queryResult(int id,String type) {
        List<TestResultMht> list = testResultMhtService.queryResult(id,type);
        if(ObjectUtil.isNotEmpty(list)){
            for (TestResultMht testResultMht : list) {
                return success(testResultMht);
            }
        }
        return fail();
    }


    /**
     * 报表统计方法
     * @param
     * @return
     */
    @RequestMapping("queryStatement.do")
    public Result queryStatement() {
        List<TestResultMht> list = testResultMhtService.queryStatement();
        if(ObjectUtil.isNotEmpty(list)){
            for (TestResultMht testResultMht : list) {
                return success(testResultMht);
            }
        }
        return fail();
    }

    @RequestMapping("getPlanId.do")
    public Result getPlanId() {
        List<Map<String, Object>> planId = testResultMhtService.getPlanId();
        if (ObjectUtil.isNotEmpty(planId)) {
            return success(planId);
        }
        return fail();
    }


    @RequestMapping("queryQuestionMHT.do")
    public Result getQuestionMHT(PageOrder pageOrder , Integer id) {
        Page<Map<String, Object>> byTesterId = testResultMhtService.getByTesterId(pageOrder, id);
        if (ObjectUtil.isNotEmpty(byTesterId)) {
            return success(byTesterId.getTotal(), byTesterId.getResult());
        }
        return fail();
    }

    @RequestMapping("queryReturn.do")
    public Object queryReturn(PageOrder pageOrder , ReturnRecords records) {
        Page<ReturnRecords> byTesterId = testResultMhtService.getReturnRecords(pageOrder, records);
        JSONConfig jsonConfig = JSONConfig.create();
        jsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        String jsonStr = JSONUtil.toJsonStr(success(byTesterId.getTotal(), byTesterId.getResult()), jsonConfig);
        return jsonStr;
    }

    @RequestMapping("insertReturn.do")
    public Result insertReturn( ReturnRecords records) {
        int i = testResultMhtService.insertReturn(records);
        if (i>0) {
            return success( );
        }
        return fail();
    }

    /**
     * 通过id删除回访记录的一条或多条数据
     * @param id
     * @return
     */
    @RequestMapping("deleteReturn.do")
    public Result delete( int[] id) {
        int i = testResultMhtService.deleteReturn(id);
        if (i>0) {
            return success( );
        }
        return fail();
    }
    /**
     * 通过id修改回访记录
     * @param returnRecords
     * @return
     */
    @RequestMapping("updateReturn.do")
    public Result updateReturn(ReturnRecords returnRecords ) {
        int i = testResultMhtService.updateReturn(returnRecords);
        if (i>0) {
            return success( );
        }
        return fail();
    }
    //用户查询页面
    @RequestMapping("toShowMHTReturn.do")
    public ModelAndView toShowMHTReturn(ModelAndView modelAndView){
        modelAndView.setViewName("mht/showMHTReturn");
        return modelAndView;
    }

    //用户查询页面
    @RequestMapping("toShowMHTResult.do")
    public ModelAndView toShowUser(ModelAndView modelAndView){
        modelAndView.setViewName("mht/showResultMHT");
        return modelAndView;
    }

    //用户查询页面
    @RequestMapping("toShowMHTStatement.do")
    public ModelAndView toShowMHTStatement(ModelAndView modelAndView){
        modelAndView.setViewName("mht/showMHTStatement");
        return modelAndView;
    }

}

